package nextoffer.gaotu;

import java.util.*;
import java.util.stream.Collectors;

public class First {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.next();
        String strs = in.next();
        if (Objects.isNull(s) || Objects.isNull(strs)) {
            System.out.println(new ArrayList<>());
            return;
        }
        String[] split = strs.split(",");
        Map<Integer, Integer> map = new HashMap<>();
        String tmp[] = new String[split.length];
        for(int i = 0; i < s.length();i++) {
            map.put((int) s.charAt(i), map.getOrDefault((int)s.charAt(i), 0) + 1);
        }
        for(int i = 0; i < split.length; i++) {
            String t = "";
            for(int j = 0 ;j < split[i].length(); j++){
                 t +=  map.getOrDefault((int)split[i].charAt(j), 0);
            }
            tmp[i] = t;
        }
        String[][] res = new String[split.length][2];
        for(int i = 0; i < split.length; i++) {
             res[i][0] = split[i];
             res[i][1] = tmp[i];
        }
        Arrays.sort(res, (a,b) -> a[1].compareTo(b[1]));
        List<String> collect = Arrays.stream(res).map(a -> a[0]).collect(Collectors.toList());
        System.out.println(Arrays.stream(res).map(a -> a[1]).collect(Collectors.toList()));

        System.out.println(collect);
    }
}
